package com.c.ctgproject.http


import com.c.ctgproject.BuildConfig
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.userToken
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 *作者:LiBW
 *创建日期: 2020/8/15
 *描述: Retrofit类
 */
open class ApiMethods {
    companion object {

        val urmRetrofit=getUrmApiService()

        val retrofit = getApiService()
        fun getApiService(): ApiService {
            val retrofit = Retrofit.Builder().baseUrl(BuildConfig.SERVER_URL)//设置网络请求的URL地址
                .addConverterFactory(GsonConverterFactory.create())//设置数据解析器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持RxJava
                .client(initClient())//拦截器
                .build()
            return retrofit.create(ApiService::class.java)
        }

        private fun getUrmApiService(): ApiService {
            val retrofit = Retrofit.Builder().baseUrl(BuildConfig.URM_URL)//设置网络请求的URL地址
                .addConverterFactory(GsonConverterFactory.create())//设置数据解析器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持RxJava
                .client(initClient())//拦截器
                .build()
            return retrofit.create(ApiService::class.java)
        }



        fun apiSubscribe(observable: Observable<ResponseBody>, observer: Observer<ResponseBody>) {
            observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }

        //拦截器
        private fun initClient(): OkHttpClient {
            //日志拦截器
            val interceptor = HttpLoggingInterceptor()
            //以前是BODY
            interceptor.level = HttpLoggingInterceptor.Level.BODY
            //header配置拦截器
            val headerInterceptor = Interceptor {
                val request = it.request().newBuilder()
                    .addHeader("Content-Type", "application/json")
                    .addHeader("charset", "utf-8")
                    .build()
                it.proceed(request)
            }

            return OkHttpClient.Builder()
                //拦截器设置
                .addInterceptor(headerInterceptor)
                .addInterceptor(interceptor)
                .connectTimeout(3, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(3, TimeUnit.MINUTES)
                .build()
        }

        var token: String = userToken

        //客服订单统计
        fun serviceOrderCount(observer: Observer<ResponseBody>) {
            apiSubscribe(retrofit.serviceOrderCount(), observer)
        }

        //门卫带入场车辆
        fun listCarWaitingEntrance(observer: Observer<ResponseBody>, pageNum: Int, pageSize: Int, plateNum: String?) {
            MyLog.d("token=$token")
            apiSubscribe(retrofit.listCarWaitingEntrance(token, pageNum, pageSize, plateNum), observer)
        }

        //修改用户密码
        fun changePassword(observer: Observer<ResponseBody>, oldPassword: String, newPassword: String) {
            apiSubscribe(urmRetrofit.changePassword(token, oldPassword, newPassword), observer)
        }

        //用户登录
        fun login(observer: Observer<ResponseBody>, body: RequestBody) {
            apiSubscribe(retrofit.login(Body = body), observer)
        }

        //角色切换
        fun charactersChange(observer: Observer<ResponseBody>, roleId: Int) {
            apiSubscribe(retrofit.characterChange(token, roleId), observer)
        }

        //车辆入场上传照片
        fun carEntrance(observer: Observer<ResponseBody>, body: RequestBody, enterPic: MultipartBody.Part) {
            apiSubscribe(retrofit.carEntrance(token, body, enterPic), observer)
        }

        //app检查更新
        fun checkUpdate(observer: Observer<ResponseBody>, curVersion: String) {
            apiSubscribe(retrofit.settingCheckUpdate(curVersion), observer)
        }
    }
}